﻿using System.DrawingCore;
using ZXing.Common;
using ZXing;
using System.DrawingCore.Imaging;

namespace LabelPrint.Utility
{
    public static class MakeTXCode
    {
        /// <summary>
        ///  生成条形码
        /// </summary>
        /// <param name="message">条码信息</param>
        /// <param name="gifFileName">生成条码图片文件名</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public static Bitmap CreateBarCode2(string message, string gifFileName, int width, int height)
        {
            var w = new ZXing.OneD.Code128Writer();
            //var w = new ZXing.OneD.CodaBarWriter();
            BitMatrix b = w.encode(message, BarcodeFormat.CODE_128, width, height);
            var zzb = new ZXing.ZKWeb.BarcodeWriter();
            zzb.Options = new EncodingOptions()
            {
                Margin = 3,
                PureBarcode = false
            };
            string dir = Path.GetDirectoryName(gifFileName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            Bitmap b2 = zzb.Write(b);
            b2.Save(gifFileName, ImageFormat.Gif);
            b2.Dispose();

            return b2;
        }
        /// <summary>
        ///  生成条形码
        /// </summary>
        /// <param name="message">条码信息</param>
        /// <param name="gifFileName">生成条码图片文件名</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public static Bitmap CreateBarCode(string message, string gifFileName, int width, int height)
        {
            var w = new ZXing.OneD.Code128Writer();
            //var w = new ZXing.OneD.CodaBarWriter();
            BitMatrix b = w.encode(message, BarcodeFormat.CODE_128, width, height);
            var zzb = new ZXing.ZKWeb.BarcodeWriter();
            zzb.Options = new EncodingOptions()
            {
                Margin = 3,
                PureBarcode = false
            };
            string dir = Path.GetDirectoryName(gifFileName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            Bitmap b2 = zzb.Write(b);
            //b2.Save(gifFileName, ImageFormat.Gif);
            //b2.Dispose();

            return b2;
        }

        /// <summary>
        ///  生成条形码
        /// </summary>
        /// <param name="message">条码信息</param>
        /// <param name="gifFileName">生成二维码图片</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public static Bitmap CreateQrCode(string message, string gifFileName, int width, int height)
        {
            var w = new ZXing.QrCode.QRCodeWriter();
            BitMatrix b = w.encode(message,BarcodeFormat.QR_CODE, width, height);
            var zzb = new ZXing.ZKWeb.BarcodeWriter();
            zzb.Options = new EncodingOptions()
            {
                Margin = 3,
                PureBarcode = false
            };
            string dir = Path.GetDirectoryName(gifFileName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            Bitmap b2 = zzb.Write(b);
            //b2.Save(gifFileName, ImageFormat.Gif);
            //b2.Dispose();

            return b2;
        }
    }
   
}
